﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Configuration;
using System.Linq;
using System.Text;
using Autorent.Desktop.Model.AutorentMngrServiceReference;

namespace Autorent.Desktop.Model.Services
{
    public class SystemUsersRepository : AutorentEntitiesContext, ISystemUsersRepository
    {      

        public ObservableCollection<SystemUser> GetSystemUsers()
        {
            try
            {
                return new ObservableCollection<SystemUser>(Context.SystemUsers);
            }
            catch (Exception)
            {
                throw new AutorentModelException("System users loading error!");
            }
        }

        public SystemUser GetSystemUserByName(string name)
        {
            SystemUser systemUser = null;

            if (Context.SystemUsers.Count() > 0)
            {
                systemUser = Context.SystemUsers.Where<SystemUser>(su => su.SystemUserName.Equals(name)).FirstOrDefault();
            }

            return systemUser;
        }

        public void AddSystemUser(SystemUser systemUser)
        {
            try
            {
                Context.AddToSystemUsers(systemUser);
                Context.SaveChanges();
            }
            catch (Exception)
            {
                throw new AutorentModelException("Add system user error occured!");
            }
        }

        public void DeleteSystemUser(SystemUser systemUser)
        {
            try
            {
                Context.DeleteObject(Context.SystemUsers.Where(su => su.SystemUser_ID.Equals(systemUser.SystemUser_ID)));
                Context.SaveChanges();
            }
            catch (Exception)
            {
                throw new AutorentModelException("Delete system user error occured!");
            }
        }
    }
}
